package top.oylan.manager.controller.admin;


import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import top.oylan.manager.entity.Category;
import top.oylan.manager.entity.extend.CategoryExtend;
import top.oylan.manager.entity.TypeCategory;
import top.oylan.manager.entity.Types;
import top.oylan.manager.service.CategoryService;
import top.oylan.manager.service.TypeService;
import top.oylan.manager.tools.Directory;
import top.oylan.manager.tools.FileUploader;
import top.oylan.manager.tools.Pager;
import top.oylan.manager.tools.Result;

import javax.annotation.Resource;
import java.util.*;

/**
 * @Author abduahad
 * @Description ${description}
 * @Date 2019-02-12
 */
@Controller
@RequestMapping("/admin/category")
public class AdminCategoryController {

    @Resource
    CategoryService categoryService;

    @Resource
    FileUploader fileUploader;

    @Resource
    TypeService typeService;

    @RequestMapping(value = {"/list","","/"})
    public String index(ModelMap modelMap, @RequestParam(value = "page",defaultValue = "1")Integer page){

        Pager<CategoryExtend> pager = new Pager<CategoryExtend>();
        pager.setCurrentPage(page);
        pager.setCondition(new CategoryExtend());
        List<CategoryExtend> list = categoryService.selectCategoryByPage(pager);
        pager.setList(list);
        modelMap.addAttribute("pager",pager);
        return "admin/category/list";
    }

    @RequestMapping("/add")
    public String add(ModelMap modelMap,
                      @RequestParam(value = "id",defaultValue = "0")Integer id){
        Category category = new Category();
        category.setParentId(0);
        List<Category> categories = categoryService.selectByExample(category);
        List<Types> types = typeService.selectAll();
        modelMap.addAttribute("types",types);
        modelMap.addAttribute("categories",categories);
        return "admin/category/add";
    }


    @RequestMapping("/edit")
    public String edit(@RequestParam(value = "id",defaultValue = "0")Integer id,
                       ModelMap modelMap){
        CategoryExtend category = categoryService.selectCategoryById(id);
        modelMap.addAttribute("category",category);

        Category categoryVO = new Category();
        category.setParentId(0);
        List<Category> categories = categoryService.selectByExample(categoryVO);
        modelMap.addAttribute("categories",categories);
        List<Types> types = typeService.selectAll();
        List<TypeCategory> typeCategories = categoryService.selectCategoryType(id);
        List<Integer> typeCategoryIds = new ArrayList<>();
        for (TypeCategory typeCategory : typeCategories){
            typeCategoryIds.add(typeCategory.getTypeId());
        }
        modelMap.addAttribute("typeCategories",typeCategoryIds);
        modelMap.addAttribute("types",types);
        return "admin/category/edit";
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @ResponseBody
    public Result save(@RequestParam(value = "id",defaultValue = "0",required = false)Integer id,
                       @RequestParam(value = "cName",defaultValue = "")String cName,
                       @RequestParam(value = "parentId",defaultValue = "0")Integer parentId,
                       @RequestParam(value = "type",defaultValue = "1")int type,
                       @RequestParam(value = "state",defaultValue = "off",required = false)String state,
                       @RequestParam(value = "localSort",defaultValue = "1")int localSort,
                       @RequestParam(value = "icon",defaultValue = "")String icon,
                       @RequestParam(value = "types[]") int[] types){

        Result result = new Result();

        Category category = new Category();
        category.setcName(cName);
        category.setParentId(parentId);
        category.setLocalSort((short)localSort);
        if(null == state || state.equals("off")){
            category.setStatus((byte)0);
        }else{
            category.setStatus((byte)1);
        }
        if(null != icon && !icon.equals("")){
           category.setIcon(icon);
        }
        try{
            if(id==0){
                category.setCreateTime(new Date());
                categoryService.saveCategory(category,types);
            }else{
                category.setId(id);
                categoryService.updateCategory(category,types);
            }
            result.setCode((byte) 1);
        }catch (Exception e){
            e.printStackTrace();
            result.setCode((byte) 0);
        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.DELETE)
    public JSONObject delete(@RequestParam(value = "id",defaultValue = "0")Integer id){

        JSONObject object = new JSONObject();
        int delId = categoryService.delete(id);
        if(delId > 0){
            object.put("status",1);
        }else{
            object.put("status",0);
        }
        return object;
    }

    @RequestMapping(value = "/byType",method = RequestMethod.GET)
    @ResponseBody
    public Result categoriesApi(@RequestParam(value = "type",defaultValue = "1") int type){

        top.oylan.manager.tools.enums.Types types = null;
        switch (type){
            case 1:
                types = top.oylan.manager.tools.enums.Types.COURSE;
                break;
            case 2:
                types = top.oylan.manager.tools.enums.Types.FILM;
                break;
            case 3:
                types = top.oylan.manager.tools.enums.Types.MOVIE;
                break;
            case 4:
                types = top.oylan.manager.tools.enums.Types.NOTICE;
                break;
            case 5:
                types = top.oylan.manager.tools.enums.Types.FILM_NOTICE;
                break;
            case 6:
                types = top.oylan.manager.tools.enums.Types.MOVIE_NOTICE;
                break;
        }

        Result result = new Result();
        List<Category> data = categoryService.selectCategoriesForProduct(types);
        Map<String, Object> map = new HashMap<>();
        map.put("categories",data);
        result.setData(map);
        result.setCode((byte) 1);
        return result;
    }

    @RequestMapping("/upload")
    @ResponseBody
    public JSONObject upload(MultipartFile file){
        JSONObject object = new JSONObject();
        Result res = fileUploader.upload(file, Directory.CATEGORY);
        if(res.getCode() == 1){
            object.put("status",1);
            object.put("path",res.getData().get("path").toString());
        }else{
            object.put("status",0);
        }
        return object;
    }
}
